<template>
  <div class="cart-card">
    <div class="card-header">
      <i class="icon-cart"></i>
      <span class="card-title">药品清单</span>
      <span class="cart-count">{{ totalItems }} 种药品</span>
    </div>
    <div class="cart-content">
      <div class="cart-items">
        <!-- 药品列表 -->
        <div v-for="(item, index) in cartItems" :key="index" class="cart-item">
          <div class="medicine-info">
            <span class="medicine-icon">{{ getMedicineIcon(item.id) }}</span>
            <span class="medicine-name">{{ item.name }}</span>
          </div>
          <div class="quantity-controls">
            <button
              class="quantity-btn minus"
              @click="onDecreaseQuantity(index)"
              :disabled="isSystemStopped"
              :title="isSystemStopped ? '系统已暂停，无法操作' : ''"
            >
              <i class="icon-minus"></i>
            </button>
            <span class="quantity-display">{{ item.quantity }}</span>
            <button
              class="quantity-btn plus"
              @click="onIncreaseQuantity(index)"
              :disabled="isSystemStopped"
              :title="isSystemStopped ? '系统已暂停，无法操作' : ''"
            >
              <i class="icon-plus"></i>
            </button>
          </div>
        </div>
      </div>

      <div class="action-buttons">
        <button
          class="btn btn-stop"
          :class="{ stopped: isSystemStopped }"
          @click="onToggleSystem"
        >
          <i class="icon-stop"></i>
          {{ isSystemStopped ? '重新挂号' : '停止' }}
        </button>
        <button
          class="btn btn-cancel"
          @click="onCancel"
          :disabled="isSystemStopped"
          :title="isSystemStopped ? '系统已暂停，无法取消' : ''"
        >
          <i class="icon-cancel"></i>
          取消就诊
        </button>
        <!-- 提交按钮：新增购物车为空禁用逻辑 -->
        <button
          class="btn btn-submit"
          :class="{ disabled: !canSubmit || isSystemStopped }"
          @click="onSubmit"
          :disabled="!canSubmit || isSystemStopped "
          :title="
            isSystemStopped ? '系统已暂停，无法提交' :
            !canSubmit ? '未满足提交条件' : ''
          "
        >
          <i class="icon-submit"></i>
          下发处方
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { watch } from 'vue';

const props = defineProps({
  cartItems: {
    type: Array,
    default: () => [] // 默认空数组，避免报错
  },
  totalItems: {
    type: Number,
    default: 0
  },
  canSubmit: {
    type: Boolean,
    default: false
  },
  isSystemStopped: { // 父组件统一管理的全局停止状态
    type: Boolean,
    default: false
  },
  medicines: {
    type: Array,
    default: () => []
  },
  patientResId: { // 新增：接收当前病人resId（供后续扩展，如处方关联）
    type: String,
    default: ''
  }
})

const emit = defineEmits(['increase-quantity', 'decrease-quantity', 'submit', 'cancel', 'toggle-system'])

watch(() => props.isSystemStopped, (newVal) => {
  if (newVal) {
    alert('系统已暂停，药品清单相关操作已禁用');
  } else {
    alert('系统恢复运行，可正常操作药品清单');
  }
}, { immediate: false }); // immediate设为false，避免组件初始化时触发

// 事件触发：增强校验，避免系统停止时触发无效事件
const onIncreaseQuantity = (index) => {
  if (props.isSystemStopped) {
    alert('系统已暂停，无法增加药品数量');
    return;
  }
  emit('increase-quantity', index)
}

const onDecreaseQuantity = (index) => {
  if (props.isSystemStopped) {
    alert('系统已暂停，无法减少药品数量');
    return;
  }
  emit('decrease-quantity', index)
}

const onSubmit = () => {
  if (props.isSystemStopped) {
    alert('系统已暂停，无法下发处方');
    return;
  }
  emit('submit')
}

// 取消就诊：仅通知父组件，由父组件统一执行接口调用和数据清理
const onCancel = () => {
  if (props.isSystemStopped) {
    alert('系统已暂停，无法取消就诊');
    return;
  }
  emit('cancel')
}

const onToggleSystem = () => emit('toggle-system')

// 获取药品图标（原有逻辑不变）
const getMedicineIcon = (medicineId) => {
  const medicine = props.medicines?.find(m => m.id === medicineId)
  return medicine ? medicine.icon : '💊'
}
</script>

<style scoped>
.cart-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
  margin-top: auto;
}
.card-header {
  display: flex;
  align-items: center;
  padding: 20px 24px 16px;
  border-bottom: 1px solid #f0f2f5;
}
.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
  margin-left: 10px;
}
.card-header i {
  font-size: 20px;
  color: #4a6cf7;
}
.cart-count {
  margin-left: auto;
  background: #4a6cf7;
  color: white;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}
.cart-content {
  padding: 20px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.cart-items {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  flex: 1;
}
.cart-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  background: #f8f9fc;
  border-radius: 12px;
  border: 2px solid #e8eaf0;
  transition: all 0.3s;
}
.cart-item:hover {
  border-color: #4a6cf7;
  transform: translateY(-2px);
}
.medicine-info {
  display: flex;
  align-items: center;
  gap: 8px;
}
.medicine-icon {
  font-size: 18px;
}
.medicine-name {
  font-weight: 500;
  color: #1a1a1a;
}
.quantity-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}
.quantity-btn {
  width: 28px;
  height: 28px;
  border: 2px solid #e8eaf0;
  background: white;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 14px;
  color: #8f9bb3;
}
.quantity-btn:hover:not(:disabled) {
  border-color: #4a6cf7;
  color: #4a6cf7;
  transform: scale(1.1);
}
.quantity-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.quantity-display {
  font-weight: 600;
  color: #1a1a1a;
  min-width: 20px;
  text-align: center;
}
.action-buttons {
  display: flex;
  gap: 16px;
  margin-left: 20px;
}
.btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 28px;
  border: none;
  border-radius: 12px;
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
  transition: all 0.3s;
  min-width: 140px;
  justify-content: center;
}
.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none !important;
}
.btn-cancel {
  background: #f8f9fc;
  color: #8f9bb3;
  border: 2px solid #e8eaf0;
}
.btn-cancel:hover:not(:disabled) {
  background: #ff6b6b;
  color: white;
  border-color: #ff6b6b;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(255, 107, 107, 0.3);
}
.btn-submit {
  background: linear-gradient(135deg, #4a6cf7, #6c8aff);
  color: white;
  border: 2px solid #4a6cf7;
  box-shadow: 0 4px 12px rgba(74, 108, 247, 0.3);
}
.btn-submit:hover:not(.disabled):not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(74, 108, 247, 0.4);
}
.btn-submit.disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}
.btn-stop {
  background: #ffd166;
  color: #8a6500;
  border: 2px solid #ffd166;
}
.btn-stop:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(255, 209, 102, 0.3);
}
.btn-stop.stopped {
  background: #0ca678;
  color: white;
  border: 2px solid #0ca678;
}
.btn-stop.stopped:hover:not(:disabled) {
  box-shadow: 0 4px 12px rgba(12, 166, 120, 0.3);
}

/* 图标样式 */
.icon-cart::before { content: "🛒"; }
.icon-minus::before { content: "−"; }
.icon-plus::before { content: "+"; }
.icon-cancel::before { content: "❌"; }
.icon-submit::before { content: "✅"; }
.icon-stop::before { content: "⏸️"; }

/* 响应式调整 */
@media (max-width: 768px) {
  .cart-content {
    flex-direction: column;
    align-items: stretch;
  }
  .cart-items {
    justify-content: center;
    margin-bottom: 20px;
  }
  .action-buttons {
    flex-direction: column;
    margin-left: 0;
    margin-top: 20px;
  }
  .btn {
    width: 100%;
  }
}
</style>